package org.september.pisces.filestore;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.socket.nio.NioServer;
import org.september.pisces.filestore.controller.FileStorePoolController;
import org.september.pisces.filestore.service.FileStoreManager;
import org.september.pisces.module.api.PiscesModule;
import org.september.pisces.module.api.WebMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 标注service，用于主工程发现插件模块
 * 文件存储，存储于本地磁盘，FTP，对象存储系统等。返回一个UUID，根据UUID可以获取文件的http协议地址。
 * @author yexinzhou
 *
 */
@Configuration
@ComponentScan(basePackageClasses = FileStoreModule.class)
public class FileStoreModule implements PiscesModule , ApplicationListener<ContextRefreshedEvent>{

	public static final String 文件存储 ="文件存储";

	private static Map<String,List<WebMenu>> moduleMap = new HashMap<>();
	static {
		List<WebMenu> menus1 = new ArrayList<>();
		menus1.add(new WebMenu("文件存储-文件存储池", FileStorePoolController.List_Page, "layui-icon-component")//icon class是个问题
				.addPermission("列表页面", PiscesFilePoolPermission.存储池列表.getUrls())
				.addPermission("添加存储池", PiscesFilePoolPermission.添加存储池.getUrls())
				.addPermission("编辑存储池", PiscesFilePoolPermission.编辑存储池.getUrls())
				.addPermission("删除存储池", PiscesFilePoolPermission.删除存储池.getUrls()));
		moduleMap.put(文件存储, menus1);
	}
	public static List<WebMenu> getWebMenus(String moduleName) {
		return moduleMap.get(moduleName);
    }

	@Autowired
	private FileStoreManager fileStoreManager;

	public FileStoreModule(){
		System.out.println(FileStoreModule.class.getName()+ " init..");
	}

	public String[] getMyBatisMapperLocation() {
		return new String[] {"classpath:"+FileStoreModule.class.getPackageName().replace(".", "/")+"/mapper/*.xml"};
	}

	public String[] getEntityPackages() {
		return new String[] {FileStoreModule.class.getPackageName()+".entity"};
    }

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if (event.getApplicationContext().getParent() == null) {
			fileStoreManager.initDefaultPool();
		}

	}

}
